/*
 * Project: BoboDesignPattern
 * 
 * File Created at 2020年1月17日
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package test.practice;

import java.util.Arrays;

/**
 * @Type A20200117_fastsort
 * @Desc
 * @author 52489
 * @date 2020年1月17日
 * @Version V1.0
 */
public class A20200117_fastsort {

    public static void main(String[] args) {
        int nums[] = { 3, 3, 3, 5, 1, 2, 4 };
        fastsort(nums, 0, nums.length - 1);
        System.out.println(Arrays.toString(nums));
    }

    /**
     * @param nums
     * @param end
     * @param start
     */
    private static void fastsort(int[] nums, int start, int end) {
        if (start < end) {
            int i = start;
            int j = end;
            int tmp = nums[i];
            while (i < j) {
                while (nums[j] > tmp && j > i) {
                    j--;
                }

                while (nums[i] <= tmp && i < j) {
                    i++;
                }
                int c = nums[i];
                nums[i] = nums[j];
                nums[j] = c;
            }
            nums[start] = nums[i];
            nums[i] = tmp;
            fastsort(nums, start, i - 1);
            fastsort(nums, i + 1, end);
        }
    }

    private static void fastsort1(int[] nums, int start, int end) {
        if (start < end) {
            int i = start;
            int j = end;
            int tmp = nums[i];
            while (i < j) {
                while (nums[j] > tmp && j > i) {
                    j--;
                }
                nums[i] = nums[j];
                while (nums[i] <= tmp && i < j) {
                    i++;
                }
                nums[j] = nums[i];
            }
            nums[i] = tmp;
            fastsort1(nums, start, i - 1);
            fastsort1(nums, i + 1, end);
        }
    }

}
